package com.rchat.pocmini.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import com.airtalkee.sdk.AirtalkeeAccount;
import com.itsdf07.alog.ALog;
import com.rchat.pocmini.PocApplication;

/**
 * Created by ljd on 2017/4/26.
 * <p>
 * 1.解决WiFi、4G切换app无法正常对讲的问题
 * 2.网络自适应切换心跳大小
 */

public class NetworkReceiver extends BroadcastReceiver {

    private NetworkChangeListener networkChangeListener;
    private NetworkInfo netInfo;
    private ConnectivityManager mConnectivityManager;
    private static final String TAG = "NetworkReceiver";
    private static NetworkReceiver instance;

    public static NetworkReceiver getInstance() {
        return instance;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        instance = this;
        String action = intent.getAction();
        ALog.d("onReceive action = %s", action);
//        AirtalkeeAccount.getInstance().NetworkClose();
        //只关注网络切换
        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            netInfo = mConnectivityManager.getActiveNetworkInfo();
            //判断当前网络可用不可用
            if (netInfo != null && netInfo.isAvailable()) {
                ALog.d("onReceive 网络可用");
//                Toast.makeText(context,"网络切换广播来了,网络连接",Toast.LENGTH_SHORT).show();
                //网络切换，SDK要先断后开
                AirtalkeeAccount.getInstance().NetworkClose();
                AirtalkeeAccount.getInstance().NetworkOpen();
                /////////////网络连接
                String name = netInfo.getTypeName();

//                if(netInfo.getType()==ConnectivityManager.TYPE_WIFI){
//                    /////WiFi网络
//                    Log.e(TAG,"WiFi网络");
//                }else if(netInfo.getType()==ConnectivityManager.TYPE_ETHERNET){
//                    /////有线网络
//                    Log.e(TAG,"有线网络");
//                }else if(netInfo.getType()==ConnectivityManager.TYPE_MOBILE){
//                    /////////3g网络
//                    Log.e(TAG,"3g网络");
//                }
            } else {
                ALog.d("onReceive 网络不可用");
                ////////网络断开
//                Toast.makeText(context,"网络切换广播来了,网络断开",Toast.LENGTH_SHORT).show();
                AirtalkeeAccount.getInstance().NetworkClose();
            }
            ALog.d("onReceive networkChangeListener = %s", networkChangeListener);

            //心跳自适应
            if (networkChangeListener != null) {
            } else {
                PocApplication.getInstance().initNetworkListener();
            }
            NetworkState networkState = isConnected(context, mConnectivityManager, netInfo);
            ALog.d("onReceive networkState = %s", networkState.name());

            networkChangeListener.networkState(networkState);
        }

    }

    public void setNetworkChangeListener(NetworkChangeListener networkChangeListener) {
        this.networkChangeListener = networkChangeListener;
    }


    /**
     * 枚举网络状态
     * NET_NO：没有网络
     * NET_2G:2g网络
     * NET_3G：3g网络
     * NET_4G：4g网络
     * NET_WIFI：wifi
     * NET_UNKNOWN：未知网络
     */
    public enum NetworkState {
        NETWORK_NO,
        NETWORK_2G,
        NETWORK_3G,
        NETWORK_4G,
        NETWORK_WIFI,
        NETWORK_UNKNOWN
    }

    public interface NetworkChangeListener {
        /**
         * 网络状态码
         *
         * @param networkCode
         */
        void networkState(NetworkState networkCode);
    }

    /**
     * 判断当前是否网络连接
     *
     * @param context
     * @return 状态码
     */
    public NetworkState isConnected(Context context, ConnectivityManager cm, NetworkInfo ni) {
        NetworkState stateCode = NetworkState.NETWORK_NO;
        if (ni != null && ni.isConnectedOrConnecting()) {
            switch (ni.getType()) {
                case ConnectivityManager.TYPE_WIFI:
                    stateCode = NetworkState.NETWORK_WIFI;
                    break;
                case ConnectivityManager.TYPE_MOBILE:
                    switch (ni.getSubtype()) {
                        case TelephonyManager.NETWORK_TYPE_GPRS: //联通2g
                        case TelephonyManager.NETWORK_TYPE_CDMA: //电信2g
                        case TelephonyManager.NETWORK_TYPE_EDGE: //移动2g
                        case TelephonyManager.NETWORK_TYPE_1xRTT:
                        case TelephonyManager.NETWORK_TYPE_IDEN:
                            stateCode = NetworkState.NETWORK_2G;
                            break;
                        case TelephonyManager.NETWORK_TYPE_EVDO_A: //电信3g
                        case TelephonyManager.NETWORK_TYPE_UMTS:
                        case TelephonyManager.NETWORK_TYPE_EVDO_0:
                        case TelephonyManager.NETWORK_TYPE_HSDPA:
                        case TelephonyManager.NETWORK_TYPE_HSUPA:
                        case TelephonyManager.NETWORK_TYPE_HSPA:
                        case TelephonyManager.NETWORK_TYPE_EVDO_B:
                        case TelephonyManager.NETWORK_TYPE_EHRPD:
                        case TelephonyManager.NETWORK_TYPE_HSPAP:
                            stateCode = NetworkState.NETWORK_3G;
                            break;
                        case TelephonyManager.NETWORK_TYPE_LTE://4G 网络
                            stateCode = NetworkState.NETWORK_4G;
                            break;
                        default:
                            stateCode = NetworkState.NETWORK_UNKNOWN;//未知网络 或者无网络连接
                    }
                    break;
                default:
                    stateCode = NetworkState.NETWORK_UNKNOWN;
            }
        }
        return stateCode;
    }

}
